package cn.zjj.org.service.impl;

import cn.zjj.basic.exception.BusinessException;
import cn.zjj.basic.service.impl.BaseServiceImpl;
import cn.zjj.basic.util.BaiduAuditUtils;
import cn.zjj.org.domain.Employee;
import cn.zjj.org.domain.Shop;
import cn.zjj.org.domain.ShopAuditLog;
import cn.zjj.org.dto.ShopDto;
import cn.zjj.org.mapper.EmployeeMapper;
import cn.zjj.org.mapper.ShopAuditLogMapper;
import cn.zjj.org.mapper.ShopMapper;

import cn.zjj.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    //员工
    @Autowired
    private EmployeeMapper employeeMapper;
    //店铺日志
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    //邮件
    @Autowired
    private JavaMailSender javaMailSender;
   /* EmailUtils emailUtils = new EmailUtils();*/

    @Override
    public void settlement(Shop shop) {
        //校验
        //1.为空校验
        if(
        StringUtils.isEmpty(shop.getName()) ||
        StringUtils.isEmpty(shop.getAddress()) ||
        StringUtils.isEmpty(shop.getTel()) ||
        StringUtils.isEmpty(shop.getShopManager().getUsername()) ||
        StringUtils.isEmpty(shop.getShopManager().getEmail()) ||
        StringUtils.isEmpty(shop.getShopManager().getPhone()) ||
        StringUtils.isEmpty(shop.getShopManager().getPassword()) ||
        StringUtils.isEmpty(shop.getShopManager().getComfirmPassword())
        ){
            throw new BusinessException("带'*'的选项不能为空");
        }


        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if(!aBoolean){ //名字违规
            throw new BusinessException("用户名不合法,请重新输入!");

        }

        //不能在上面抽,上面抽出没判断为空,可能会sql执行错误
        Employee emp = shop.getShopManager();
        //2.两次密码校验
        if(!emp.getPassword().equals(emp.getComfirmPassword())){
            throw new BusinessException("两次密码输入不一致,请重新输入");
        }

        //3.店铺是否注册校验
        Shop msgShop = shopMapper.getShoByMessage(shop);
        if(msgShop!=null){
            throw new BusinessException("该店铺已经注册过啦,请直接登录!");
        }


        //业务都没错误,开始执行业务代码
        //添加employee   添加完就会返回新增的id  当前对象就会有id sql中配置
        employeeMapper.insert(emp);

        //把返回的emp  id  添加到shop的admin_id字段中
        shop.setAdmin_id(emp.getId());

        //添加shop  添加完就会返回新增的id 当前对象就会有id sql中配置
        shopMapper.insert(shop);   //先把admin_id 添加进shop了再添加,不用再去更新一次了
        //把返回的 shop id 添加到 emp的shop_id字段中
        emp.setShop_id(shop.getId());   //emp的添加操作已经执行了,这儿添加的shop_id只在当前对象,还没更新到数据库

        employeeMapper.update(emp);//更新到数据库

    }

    //入驻人工审核
    @Override
    public void audit(ShopAuditLog shopAuditLog, Boolean auditType) throws MessagingException {
        Long shop_id = shopAuditLog.getShop_id();
        //根据shopid查询该shop
        Shop shop = shopMapper.selectById(shop_id);
        String text;
        //修改状态
        if(auditType){
            shop.setState(2);
            shopAuditLog.setState(2);
            text="<h1>恭喜您,成功入驻宠物之家,请点击链接激活账户</h1>" +
                     "<img  width='80px' src='http://123.207.27.208"+shop.getLogo()+"'>" +
                    "<a href='http://localhost:8080/shop/active/"+shopAuditLog.getShop_id()+"'>点击该链接激活</a>";
        }else{
            shop.setState(4);
            shopAuditLog.setState(4);
            text="<h1>您的入驻申请被驳回,请查看详情后重试</h1><br/>" +
                    "<h3>"+shopAuditLog.getNote()+"</h3>" +
                    "<img width='80px' src='http://123.207.27.208"+shop.getLogo()+"' />";
        }
        //更新当前shop状态
        shopMapper.update(shop);
        //审核人员,没有做登录,暂时随便写个
        shopAuditLog.setAudit_id(1L);
        //添加到店铺日志数据库
        shopAuditLogMapper.insert(shopAuditLog);

        //发送邮件通知商家激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("3023850005@qq.com");
        helper.setSubject("宠物之家入驻申请通知!");
        helper.setText(text,true);
        /*//添加附件
        helper.addAttachment("aa.jpg",new File("C:\\Users\\30238\\Desktop\\aa.jpg"));*/

        //获取收件人邮箱
        String email = employeeMapper.selectById(shop.getAdmin_id()).getEmail();
        //收件人
        /*helper.setTo(email);*/
        helper.setTo("3023850005@qq.com");
        //发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }


    //审核成功
//    @Override
//    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException {
//        Long shop_id = shopAuditLog.getShop_id();
//        //根据shopid查询该shop
//        Shop shop = shopMapper.selectById(shop_id);
//        //修改状态
//        shop.setState(2);
//        //更新当前shop状态
//        shopMapper.update(shop);
//
//        //添加到店铺日志数据库
//        shopAuditLog.setState(2);
//        shopAuditLog.setAudit_id(1L);
//        shopAuditLogMapper.insert(shopAuditLog);
//
//        //发送邮件通知商家激活
//        //创建复杂邮件对象
//        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//        //发送复杂邮件的工具类
//        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
//        helper.setFrom("3023850005@qq.com");
//        helper.setSubject("宠物之家店铺入驻审核邮件!");
//        helper.setText("<h1>你的店铺已经注册!!!</h1><img  width='80px' src='http://123.207.27.208/group1/M00/00/F4/CgAIC2LdKryAO58EAAA5-iILacU996.jpg' ><a href='http://localhost:8080/shop/active/"+shopAuditLog.getShop_id()+"'>点击该链接激活</a>",true);
//        /*//添加附件
//        helper.addAttachment("aa.jpg",new File("C:\\Users\\30238\\Desktop\\aa.jpg"));*/
//
//        //获取收件人邮箱
//        Employee employee = employeeMapper.selectById(shop.getAdmin_id());
//        //收件人
//        /*helper.setTo(employee.getEmail());*/
//        helper.setTo("3023850005@qq.com");
//        //发送
//        javaMailSender.send(mimeMessage);
//
//
///*        emailUtils.sendEmail("恭喜您,成功入驻宠物之家!","<h1>你的店铺已经注册!!!</h1><img " +
//                        "src='http://123.207.27.208/group1/M00/00/F4/CgAIC2LdKryAO58EAAA5-iILacU996.jpg' >" +
//                        "<a href='http://localhost:8080/shop/active/"+shopAuditLog.getShop_id()+"'>点击该链接激活</a>",
//                employeeMapper.selectById(shop.getAdmin_id()).getEmail());*/
//
//    }
//
//    //驳回成功
//    @Override
//    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
//        Long shop_id = shopAuditLog.getShop_id();
//        //根据shopid查询该shop
//        Shop shop = shopMapper.selectById(shop_id);
//        //修改状态
//        shop.setState(4);
//        //更新当前shop状态
//        shopMapper.update(shop);
//
//        //添加到店铺日志数据库
//        shopAuditLog.setState(4);
//        shopAuditLog.setAudit_id(1L);
//        shopAuditLogMapper.insert(shopAuditLog);
//
//        //发送邮件通知商家激活
//        //创建复杂邮件对象
//        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//        //发送复杂邮件的工具类
//        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
//        helper.setFrom("3023850005@qq.com");
//        helper.setSubject("宠物之家店铺入驻审核邮件!");
//        helper.setText("<h1>你的入驻申请被驳回!!</h1><br/><h3>"+shopAuditLog.getNote()+"</h3><img width='80px' src='http://123.207.27.208/group1/M00/00/F4/CgAIC2LdKryAO58EAAA5-iILacU996.jpg' />",true);
//        /*//添加附件
//        helper.addAttachment("aa.jpg",new File("C:\\Users\\30238\\Desktop\\aa.jpg"));*/
//
//        //获取收件人邮箱
//        Employee employee = employeeMapper.selectById(shop.getAdmin_id());
//        //收件人
//        /*helper.setTo(employee.getEmail());*/
//        helper.setTo("3023850005@qq.com");
//        //发送
//        javaMailSender.send(mimeMessage);
//
///*        emailUtils.sendEmail("入驻申请失败!","<h1>你的入驻申请被驳回!!</h1><br/>" +
//                        "<h3>"+shopAuditLog.getNote()+"</h3>" +
//                        "<img width='80px' src='http://123.207.27.208/group1/M00/00/F4/CgAIC2LdKryAO58EAAA5-iILacU996.jpg' />",
//                employeeMapper.selectById(shop.getAdmin_id()).getEmail());*/
//
//    }



}
